package com.anlogic.sdk.toolpreference.utils;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.widgets.Text;
import org.osgi.service.prefs.BackingStoreException;

public class MifPreferencesUtils {
	public static final String PLUGIN_ID = "com.anlogic.sdk.createmif";

	public static IEclipsePreferences getPreferences() {
		IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
		return preferences;
	}
	public static void savePreferences(ModifyEvent e) {
		IEclipsePreferences mifPrefs=getPreferences();
		Text text = (Text) e.getSource();
		String textId = (String) text.getData();
		String textContent = text.getText();
		if ("inputText".equals(textId)) {
			mifPrefs.put("inputText", textContent);
		} else if ("romOffsetText".equals(textId)) {
			mifPrefs.put("romOffsetText", textContent);
		} else if ("romLengthText".equals(textId)) {
			mifPrefs.put("romLengthText", textContent);
		}
		try {
			mifPrefs.flush();
		} catch (BackingStoreException e1) {
			e1.printStackTrace();
		}
	}

	public static String[] loadPreferences(String... value) {
		IEclipsePreferences mifPrefs=getPreferences();
		String[] s = new String[3];
		String inputPath = mifPrefs.get("inputText", value[0]);
		if(inputPath==null) {
			inputPath="";
		}
		s[0] = inputPath;
		String romLength = mifPrefs.get("romLengthText", value[1]);
		s[1] = romLength;
		String romOffset = mifPrefs.get("romOffsetText", "0");
		s[2] = romOffset;
		return s;

	}
}
